package com.tsing.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.wms.config.RedisManager;
import com.tsing.wms.constant.AuthConsts;
import com.tsing.wms.entity.GoodsEntity;
import com.tsing.wms.entity.StockEntity;
import com.tsing.wms.entity.StorehouseEntity;
import com.tsing.wms.exception.CommonException;
import com.tsing.wms.exception.ServerException;
import com.tsing.wms.mapper.GoodsMapper;
import com.tsing.wms.mapper.StockMapper;
import com.tsing.wms.mapper.StorehouseMapper;
import com.tsing.wms.service.IStockService;
import com.tsing.wms.util.GetLoginUserUtil;
import com.tsing.wms.vo.PageResultVO;
import com.tsing.wms.vo.req.IdReqVO;
import com.tsing.wms.vo.req.StockReqVO;
import com.tsing.wms.vo.req.SupplierPageReqVO;
import com.tsing.wms.vo.resp.StockRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author TheTsing
 * @create 2022-04-17 15:31
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, StockEntity> implements IStockService {

    @Autowired
    private RedisManager redisManager;

    @Override
    public void addStock(StockReqVO reqVO, HttpServletRequest request) {
        if (GetLoginUserUtil.getLoginUserInfo(request.getHeader(AuthConsts.AUTHENTICATION_HEAD), redisManager).getIdentity() != 1) {
            throw new ServerException(CommonException.Proxy.AUTHORIZATION_EXCEPTION);
        }
        if (ObjectUtil.isNotNull(new LambdaQueryChainWrapper<>(baseMapper)
                .select(StockEntity::getId)
                .eq(StockEntity::getGoodsId, reqVO.getGoodsId())
                .eq(StockEntity::getStorehouseId, reqVO.getStorehouseId())
                .one())) {
            throw new ServerException(CommonException.Proxy.STOCK_IS_NOTNULL);
        }
        StockEntity stockEntity = new StockEntity();
        BeanUtil.copyProperties(reqVO, stockEntity);
        baseMapper.insert(stockEntity.setAmount(0));
    }

    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private StorehouseMapper storehouseMapper;

    @Override
    public StockRespVO getStock(IdReqVO reqVO) {
        StockEntity stockEntity = baseMapper.selectById(reqVO.getId());
        StockRespVO stockRespVO = new StockRespVO();
        BeanUtil.copyProperties(stockEntity, stockRespVO);
        stockRespVO.setGoodsName(new LambdaQueryChainWrapper<>(goodsMapper)
                .select(GoodsEntity::getName)
                .eq(GoodsEntity::getId, stockRespVO.getGoodsId())
                .one()
                .getName());
        stockRespVO.setStorehouseName(new LambdaQueryChainWrapper<>(storehouseMapper)
                .select(StorehouseEntity::getName)
                .eq(StorehouseEntity::getId, stockRespVO.getStorehouseId())
                .one()
                .getName());
        return stockRespVO;
    }

    @Override
    public void updateStock(StockReqVO reqVO, HttpServletRequest request) {
        if (GetLoginUserUtil.getLoginUserInfo(request.getHeader(AuthConsts.AUTHENTICATION_HEAD), redisManager).getIdentity() != 1) {
            throw new ServerException(CommonException.Proxy.AUTHORIZATION_EXCEPTION);
        }
        StockEntity stockEntity = new StockEntity();
        BeanUtil.copyProperties(reqVO, stockEntity);
        baseMapper.updateById(stockEntity.setAmount(0));
    }

    @Override
    public PageResultVO<StockRespVO> stockPage(SupplierPageReqVO reqVO) {
        IPage<StockRespVO> page = new Page<>(reqVO.getPageNum(), reqVO.getPageSize());
        baseMapper.stockPage(page, reqVO);
        return new PageResultVO<>(page);
    }

}
